//
//  AppDelegate.m
//  Fengshui
//
//  Created by are on 13-5-22.
//
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "SoftSettingViewController.h"
#import "SinaWeibo.h"

#import "CollectViewController.h"
#import "ShareViewController.h"
@implementation AppDelegate
@synthesize sinaweibo;
@synthesize sinaOrWeixin = _sinaOrWeixin;
 
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
   // UITabBarController *tab = [[UITabBarController alloc] init];

    //-------------------- 首页 --------------------//
    self.mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
    self.window.rootViewController = nav1;
    [self.mainViewController release];

    
//    ShareViewController *shareViewController = [[ShareViewController alloc] initWithNibName:@"ShareViewController" bundle:nil];
//    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:shareViewController];
//    [shareViewController release];
//
//    CollectViewController *collectViewController = [[CollectViewController alloc] initWithNibName:@"CollectViewController" bundle:nil];
//    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:collectViewController];
//    [collectViewController release];
//
//    SoftSettingViewController *softSettingViewController = [[SoftSettingViewController alloc] initWithNibName:@"SoftSettingViewController" bundle:nil];
//    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:softSettingViewController];
//    [softSettingViewController release];
//
//
//    UITabBarItem *tabBarItem;
//    tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的首页" image:[UIImage imageNamed:@"index"] tag:1];
//    nav1.tabBarItem = tabBarItem;
//    [tabBarItem release];
//    tabBarItem = [[UITabBarItem alloc] initWithTitle:@"分享" image:[UIImage imageNamed:@"friends"] tag:2];
//    nav2.tabBarItem = tabBarItem;
//    [tabBarItem release];
//
//    tabBarItem = [[UITabBarItem alloc] initWithTitle:@"收藏" image:[UIImage imageNamed:@"01-01dj"] tag:3];
//    nav3.tabBarItem = tabBarItem;
//    [tabBarItem release];
//
//    tabBarItem = [[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"me"] tag:4];
//    nav4.tabBarItem = tabBarItem;
//    [tabBarItem release];
//
//    tab.viewControllers = [NSArray arrayWithObjects:nav1, nav2,nav3,nav4,nil];
//
//    [nav1 release];
//    [nav2 release];
//    [nav3 release];
//    [nav4 release];
// 
//   // [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];

    sinaweibo = [[SinaWeibo alloc] initWithAppKey:kAppKey appSecret:kAppSecret appRedirectURI:kAppRedirectURI andDelegate:self.mainViewController];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *sinaweiboInfo = [defaults objectForKey:@"SinaWeiboAuthData"];
    if ([sinaweiboInfo objectForKey:@"AccessTokenKey"] && [sinaweiboInfo objectForKey:@"ExpirationDateKey"] && [sinaweiboInfo objectForKey:@"UserIDKey"])
    {
        sinaweibo.accessToken = [sinaweiboInfo objectForKey:@"AccessTokenKey"];
        sinaweibo.expirationDate = [sinaweiboInfo objectForKey:@"ExpirationDateKey"];
        sinaweibo.userID = [sinaweiboInfo objectForKey:@"UserIDKey"];
    }

    [WXApi registerApp:@"wxf34ec3e5444eefdc"];

    [self.window makeKeyAndVisible];
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self.sinaweibo applicationDidBecomeActive];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
-(void) onResp:(BaseResp*)resp
{
    if([resp isKindOfClass:[SendMessageToWXResp class]])
    {
       
        if (resp.errCode == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil
                                                        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
            
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:nil
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
        
    }
    else if([resp isKindOfClass:[SendAuthResp class]])
    {
        NSString *strTitle = [NSString stringWithFormat:@"Auth结果"];
        NSString *strMsg = [NSString stringWithFormat:@"Auth结果:%d", resp.errCode];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
         
    }
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if (!self.sinaOrWeixin) {

        return [self.sinaweibo handleOpenURL:url];
    } 
    else
    {
        return  [WXApi handleOpenURL:url delegate:self];
    }
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if (!self.sinaOrWeixin) {

        return [self.sinaweibo handleOpenURL:url];
    }
    else
    {
        return  [WXApi handleOpenURL:url delegate:self];
    }
}

@end
